package com.clean.cleantotal.base

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding

abstract class BaseFragment<T : ViewBinding> : Fragment() {
    protected lateinit var mBinding: T
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        mBinding = getViewBinding(inflater, container)
        return mBinding.root
    }

    abstract fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): T

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initParams(arguments)
        initView()
        initData(savedInstanceState)
    }

    abstract fun initView()
    abstract fun initData(savedInstanceState: Bundle?)

    open fun initParams(arguments: Bundle?) {}
    protected fun openActivity(activity: Class<*>?, bundle: Bundle? = null) {
        bundle?.let {
            startActivity(Intent(requireContext(), activity).apply { putExtras(it) })
        } ?: kotlin.run {
            startActivity(Intent(requireContext(), activity))
        }
    }
}